import { toast } from 'sonner'

export function getResponseErrorMessage(data: unknown) {
  if (typeof data === 'object' && data !== null) {
    if ('error' in data) {
      return String(data.error)
    }
    if ('message' in data) {
      return String(data.message)
    }
  }
  return String(data)
}

export function apiErrorToast(data: unknown) {
  const errorMessage = getResponseErrorMessage(data)
  if (errorMessage) {
    toast.error(errorMessage)
  }
}
